package com.wxzz.elearing.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxzz.elearing.user.dto.SubToolFormDelDto;
import com.wxzz.elearing.user.dto.SubToolFormDto;
import com.wxzz.elearing.user.entity.SubToolForm;
import com.wxzz.elearing.user.entity.ToolForm;
import com.wxzz.elearing.user.mapper.SubToolFormMapper;
import com.wxzz.elearing.user.service.ISubToolFormService;
import com.wxzz.elearning.common.exception.ServiceException;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2021-05-18
 */
@Service
public class SubToolFormServiceImpl extends ServiceImpl<SubToolFormMapper, SubToolForm> implements ISubToolFormService {

    @Resource
    private SubToolFormMapper subToolFormMapper;

    public ReturnVO addSubToolForm(SubToolForm toolForm) {
        if(toolForm.getId()==null){
            subToolFormMapper.insert(toolForm);
            return new ReturnVO("二级工具表单添加成功", null);
        }
        subToolFormMapper.updateById(toolForm);
        return new ReturnVO("二级工具表单修改成功",null);
    }

    public ReturnVO<JgPageVo<List<SubToolForm>>> subToolFormList(SubToolFormDto toolFormDto) {
        PageHelper.startPage(toolFormDto.getCurPage(), toolFormDto.getMaxPage());
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id",toolFormDto.getParentId());
        queryWrapper.eq("enterprise_id",toolFormDto.getEnterpriseId());
        if(StringUtils.isNotBlank(toolFormDto.getSubToolFormName())){
            queryWrapper.like("sub_tool_form_name",toolFormDto.getSubToolFormName());
        }
        queryWrapper.orderByDesc("create_time");
        List<SubToolForm> list = subToolFormMapper.selectList(queryWrapper);
        return new ReturnVO("工具表单列表查询成功！",new JgPageVo(new PageInfo<>(list)));
    }

    public ReturnVO deleteSubToolForm(SubToolFormDelDto toolForm){
        int i = subToolFormMapper.deleteBatchIds(toolForm.getSubToolFormIds());
        return new ReturnVO(i+"个二级工具表单删除成功",null);
    }
}
